package com.palrevo.utils;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import com.palrevo.common.Constants;

/**
 * 
 * @author AAsha
 * 
 */
public abstract class EmailUtils {

	private static final String TEXT_HTML_UTF8 = "text/html; charset=UTF-8";

	public enum EmailType {
		TEXT, HTML;
	}

	/**
	 * 
	 * @param from
	 * @param to
	 * @param subject
	 * @param body
	 * @param emailType
	 */
	public static void sendEmail(String fromEmail, String fromAlias,  String to, String subject, String body,
			EmailType emailType){
		sendEmail(fromEmail, fromAlias, to, subject, body, emailType, null);
	}

	/**
	 * 
	 * @param from
	 * @param to
	 * @param subject
	 * @param body
	 * @param emailType
	 * @param attachment
	 * 
	 */
	public static void sendEmail(String fromEmail, String fromAlias, String to, String subject, String body,
			EmailType emailType, File attachment)  {

		
		String host = Configs.getConfigValue(Constants.Configs.EMAIL_HOST);
		String port = Configs.getConfigValue(Constants.Configs.EMAIL_PORT);
		String mailUser = Configs.getConfigValue(Constants.Configs.MAIL_USER);
		String recipient = Configs.getConfigValue(to);
		;
		String mailPassword = Configs
				.getConfigValue(Constants.Configs.MAIL_PASSWORD);
		;

		// Get system properties
		Properties properties = System.getProperties();

		// Setup mail server
		properties.setProperty(Constants.Configs.EMAIL_HOST, host);
		properties.setProperty(Constants.Configs.EMAIL_PORT, port);

		// Setup Authentication
		properties.setProperty(Constants.Configs.MAIL_USER, mailUser);
		properties.setProperty(Constants.Configs.MAIL_PASSWORD, mailPassword);

		// Get the default Session object.
		Session session = Session.getDefaultInstance(properties);

		try {
			// Create a default MimeMessage object.
			MimeMessage message = new MimeMessage(session);

			// Set From: header field of the header.
			message.setFrom(new InternetAddress(fromEmail, fromAlias, "UTF8"));

			// Set To: header field of the header.
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(
					recipient));

			// Set Subject: header field
			message.setSubject(subject, "UTF8");
			
			message.addHeader("Content-Type", "text/html; charset=UTF-8");
			
			message.setSentDate(new Date());

			// creates multi-part
			Multipart multipart = new MimeMultipart();
			MimeBodyPart messageBodyPart = new MimeBodyPart();

			switch (emailType) {
			case TEXT:
				messageBodyPart.setText(body);
				break;
			case HTML:
				messageBodyPart.setContent(body, TEXT_HTML_UTF8);
				break;
			}

			multipart.addBodyPart(messageBodyPart);

			// adds attachments
			if (attachment != null) {
				MimeBodyPart attachPart = new MimeBodyPart();

				try {
					attachPart.attachFile(attachment);
				} catch (IOException ex) {
					ex.printStackTrace();
					Log.error(EmailUtils.class, ex.getMessage());
				}

				multipart.addBodyPart(attachPart);

			}

			// Combine message parts
			message.setContent(multipart);

			// Send message
			Transport.send(message);
			Log.info(EmailUtils.class, "Message Sent successfully..");
		} catch (MessagingException ex) {
			ex.printStackTrace();
			Log.info(EmailUtils.class, ex.getMessage());
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			Log.info(EmailUtils.class, e.getMessage());
		}
	}

}
